<template>
  <div class="content">
    <div class="common-seach-wrap" :style="{ marginBottom: '10px' }">
      <el-button type="primary" icon="Plus" @click="handleOpen('add')">新增负责人</el-button>
      <el-input
        placeholder="请输入用户名或姓名"
        v-model="keyword"
        @input="getTableInfo()"
      ></el-input>
    </div>
    <!--搜索表单-->
    <el-table :data="tableData" row-key="manager_id" height="100%" stripe :border="true">
      <el-table-column prop="manager_id" label="ID" fixed="left" width="50"></el-table-column>
      <el-table-column prop="" label="头像">
        <template #default="scope">
          <el-avatar
            :src="scope.row.avatarUrl"
            shape="circle"
            :title="scope.row.name.substring(0, 1)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="phone" label="手机号" />
      <el-table-column label="操作" fixed="right" width="400">
        <template #default="scope">
          <el-button type="success" icon="Key" @click="handlePsw(scope.row)"> 重置密码 </el-button>
          <el-button type="primary" icon="Edit" @click="handleOpen('edit', scope.row)">
            编辑
          </el-button>
          <el-button type="danger" icon="DeleteFilled" @click="delInfo(scope.row.manager_id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-divider border-style="none" />
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      background
      :current-page="curPage"
      :page-size="pageSize"
      layout="total, sizes,prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <Dialog ref="infoDrawer" @doneSave="getTableInfo" />
    <Psw ref="pswDrawer" />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, defineEmits, reactive, watch, provide } from "vue";
import Dialog from "./component/dialogForm.vue";
import Psw from "./component/psw.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { deleteManagerApi, getManagerListApi } from "@/api/babycar";
const total = ref(0);
const curPage = ref(1);
const pageSize = ref(20);
const tableData = ref([]);
const infoDrawer = ref<any>(null);
const pswDrawer = ref<any>(null);
const keyword = ref("");
onMounted(() => {
  getTableInfo();
});
const handleSizeChange = (val: number) => {
  pageSize.value = val;
  getTableInfo();
};
const handleCurrentChange = (val: number) => {
  curPage.value = val;
  getTableInfo();
};
function getTableInfo() {
  getManagerListApi({
    page: curPage.value,
    limit: pageSize.value,
    keyword: keyword.value,
  }).then((res) => {
    tableData.value = res.data.list;
    total.value = res.data.total;
  });
}

function delInfo(id: number) {
  ElMessageBox.confirm("确认要删除该负责人吗?", "警告！", {
    confirmButtonText: "确认删除",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteManagerApi(id).then((res: any) => {
        getTableInfo();
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "删除已取消",
      });
    });
}

// 打开添加数据窗口
const emits = defineEmits(["openForm", "search"]);
function handleOpen(type: string, row?: any) {
  infoDrawer.value.open(type, row);
}
function handlePsw(row: any) {
  pswDrawer.value.open(row);
}
defineExpose({
  getTableInfo,
});
</script>

<style lang="scss">
.common-seach-wrap {
  display: flex;
  align-items: center;
  justify-content: space-between;
  .el-input {
    width: 300px;
  }
}
.el-tabs {
  .el-tabs__new-tab {
    width: auto !important;
  }
}
.table-box {
  height: calc(100% - 200px);
}
</style>
